package com.msb.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.exception.BizErrorCodeEnum;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.dao.product.CategoryBrandRelationDao;
import com.msb.mall.core.entity.product.CategoryBrandRelationEntity;
import com.msb.mall.product.service.CategoryBrandRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
@Slf4j
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> findCategoryListByBrand(Long brandId) {
        return baseMapper.findCatelogListByBrand(brandId);
    }

    @Override
    public CommonResult saveSingle(CategoryBrandRelationEntity categoryBrandRelation) {
        CommonResult commonResult = CommonResult.success();
        List<CategoryBrandRelationEntity> list = getCategoryBrandRelationList(categoryBrandRelation);
        if (CollectionUtils.isEmpty(list)) {
            baseMapper.insert(categoryBrandRelation);
        } else {
            log.info("已关联过，默认通过...");
        }

        return commonResult;
    }

    @Override
    public CommonResult updateSingle(CategoryBrandRelationEntity categoryBrandRelation) {
        CommonResult commonResult = CommonResult.success();
        List<CategoryBrandRelationEntity> list = getCategoryBrandRelationList(categoryBrandRelation);
        if (CollectionUtils.isEmpty(list)) {
            baseMapper.insert(categoryBrandRelation);
        } else {
            commonResult.setCode(BizErrorCodeEnum.VALID_EXCEPTION.getCode());
            commonResult.setMsg("品牌分类关联关系已存在！");
        }

        return commonResult;
    }

    @Override
    public List<CategoryBrandRelationEntity> categoryBrandRelation(Long catId) {
        return baseMapper.categoryBrandRelation(catId);
    }

    public List<CategoryBrandRelationEntity> getCategoryBrandRelationList(CategoryBrandRelationEntity categoryBrandRelation) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryBrandRelation.getBrandId() != null, CategoryBrandRelationEntity::getBrandId, categoryBrandRelation.getBrandId())
                .eq(categoryBrandRelation.getCatelogId() != null, CategoryBrandRelationEntity::getCatelogId, categoryBrandRelation.getCatelogId());
        return baseMapper.selectList(queryWrapper);
    }

}